package com.example.dog.core.aspect;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.context.annotation.Configuration;

@Aspect
@Slf4j
@Configuration
//@Component
public class TestAspect {
    private final String POINT_CUT = "execution(public * com.example.dog.*.*(..))";
    private final String POINT_CUT2 = "execution(* com.example.dog.*.*(..))";
    private final String POINT_CUT1 = "@annotation(com.example.dog.core.annotation.SysLog)";
    /**
     * 这里我们使用注解的形式
     * 当然，我们也可以通过切点表达式直接指定需要拦截的package,需要拦截的class 以及 method
     * 切点表达式:   execution(...)
     */
    @Pointcut(POINT_CUT1)
    public void logPointCut() {
        log.info("没问题");
    }
    @Before("logPointCut()")
    public void before(){
        System.out.println("在执行dao中实现类之前调用");
    }
    @Around("logPointCut()")
    public Object around(ProceedingJoinPoint point) throws Throwable {
        String strClassName = point.getTarget().getClass().getName();
        String strMethodName = point.getSignature().getName();
        log.info("[类名]:{},[方法]:{},开始", strClassName, strMethodName);
        Object obj = point.proceed();
        log.info("[类名]:{},[方法]:{},结束", strClassName, strMethodName);
        return "不合法调用，已被系统拦截";
    }
    @Before("logPointCut()")
    public void after(){
        System.out.println("在执行dao中实现类之后调用");
    }
}
